package com.chun.learning.analyzation.controller;

import com.chun.learning.analyzation.mapper.AnalyzeMapper;
import com.chun.learning.analyzation.service.AnalyzeService;
import com.lin.learning.questionmanagement.service.UploadService;
import com.lin.learning.questionmanagement.vo.FileVo;
import com.our.learning.infrastructure.po.WrongQuestionReport;
import com.our.learning.infrastructure.pojo.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.websocket.server.PathParam;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/analyze")
public class AnalyzeController {

    @Autowired
    private  AnalyzeService analyzeService;

    @Autowired
    private UploadService uploadService;

    @Autowired
    private AnalyzeMapper analyzeMapper;


    @GetMapping("/getadminreport")
    public ResponseResult getAdminReport(){
        return analyzeService.getAdminReport();
    }

    @RequestMapping("/generatewrongreport")
    public ResponseResult generateWrongReport(@PathParam("stuid") Integer stuid, @PathParam("examid") Integer examid){
        return analyzeService.generateWrongReport(stuid,examid);
    }

    @GetMapping("/getwrongreport")
    public ResponseResult getWrongReport(@PathParam("stuid") Integer stuid){
        return analyzeService.getWrongReport(stuid);
    }

    @PostMapping("/upload")
    public ResponseResult uploadReport(@RequestParam("file") MultipartFile multipartFile,@RequestParam("stuid") Integer stuid,@RequestParam("examid") Integer examid){
        try{
            //在generateWrongReport方法中已经在数据库建立了错误报告
            ResponseResult upload = uploadService.upload(multipartFile);
            FileVo file = (FileVo) upload.getData();
            String data=file.getUrl();
            //录入生成的错题报告链接
            analyzeMapper.alterLink(stuid,examid,data);
            return upload;
        }catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    @GetMapping("/test")
    public  ResponseResult test(){
        List<String> list= Arrays.asList("a","b","c");
        return ResponseResult.okResult(list);
    }

    @GetMapping("/download")
    public ResponseResult downloadQuestionOrPdf(String path){
        try{
            return uploadService.downloadOss(path);
        }catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException("文件上传失败");
        }
    }

    @GetMapping("/getallreports")
    public ResponseResult getAllReports(String stuid){
        return analyzeService.getStudentAllReports(stuid);
    }

    @GetMapping("/getclassreports")
    public ResponseResult getClassReports(Integer classid){
        return analyzeService.getClassReports(classid);
    }


    @DeleteMapping("/deletereport")
    public ResponseResult deleteReport(@PathParam("stuid") Integer stuid, @PathParam("examid") Integer examid){
        return analyzeService.deleteReport(stuid,examid);
    }
/*    @GetMapping("/getonereport")
    public ResponseResult getOneReport(String stuid){
        return analyzeService.getOneReport(stuid);
    }*/
}
